package com.totoro.service.impl;

import com.totoro.mapper.ReturnMapper;
import com.totoro.pojo.*;
import com.totoro.service.ReturnService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;

/**
 * @author TOTORO
 * @date 2021/8/9 23:50
 */
@Service
public class ReturnServiceImpl implements ReturnService {

    @Autowired
    private ReturnMapper returnMapper;

    @Override
    public List<PrimaryReturn> findUserReturnList(int uid, int type, int time, int status, String startTime, String endTime) {
        return returnMapper.findUserReturnList(uid, type, time, status, startTime, endTime);
    }

    @Override
    public Integer addPrimaryReturn(PrimaryReturn primaryReturn) {
        return returnMapper.addPrimaryReturn(primaryReturn);
    }

    @Override
    public Integer updatePrimaryReturn(PrimaryReturn primaryReturn) {
        return returnMapper.updatePrimaryReturn(primaryReturn);
    }

    @SneakyThrows
    @Override
    public Integer addPrimaryReturnImage(Integer primaryReturnId, Integer returnStatus, MultipartFile[] files, String[] instructions, HttpServletRequest request) {

        if (files != null && files.length > 0) {
            //循环获取file数组中得文件
            for (int i = 0; i < files.length; i++) {
                MultipartFile file = files[i];
                String uploadPath = request.getSession().getServletContext().getRealPath("/")+"static\\img\\tmall_return\\";
                File path =  new File(uploadPath);
                if(path == null){ path.mkdirs();}
                String filename = UUID.randomUUID().toString();
                // 通过MultipartFile的transferTo(File dest)这个方法来直接转存文件到指定的路径
                String ext =  file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
                System.err.println(ext);
                file.transferTo(new File(uploadPath+filename+file.getOriginalFilename()));
                // 打包成对象，交互sql
                PrtImages prtImages = new PrtImages();
                prtImages.setPrimaryReturnId(primaryReturnId);
                prtImages.setReturnStatus(returnStatus);
                prtImages.setImageCode(i+1);
                prtImages.setImageInstruction(instructions[i]);
                prtImages.setImagePath("\\static\\img\\tmall_return\\"+filename+file.getOriginalFilename());
                // 添加
                returnMapper.addPrimaryReturnImage(prtImages);
            }
        }
        return null;
    }

    @Override
    public Integer deletePrimaryReturnAssignStatusImages(int prId, int status) {
        return returnMapper.deletePrimaryReturnAssignStatusImages(prId, status);
    }

    @Override
    public Integer deletePrimaryReturnOneStatusImages(int prId, Integer[] originalImageId) {
        return returnMapper.deletePrimaryReturnOneStatusImages(prId, originalImageId);
    }

    @Override
    public List<PrtImages> findShallQuiltRemoveMultipleImage(int prId, Integer[] originalImageId) {
        return returnMapper.findShallQuiltRemoveMultipleImage(prId, originalImageId);
    }

    @Override
    public Integer updatePrimaryReturnAssignStatusImageInstructionByImageId(int piId, String instruction) {
        return returnMapper.updatePrimaryReturnAssignStatusImageInstructionByImageId(piId, instruction);
    }

    @Override
    public Integer addPrimaryReturnAssignStatusInfo(ReturnStatusInfo returnStatusInfo) {
        return returnMapper.addPrimaryReturnAssignStatusInfo(returnStatusInfo);
    }

    @Override
    public Integer addNegotiateHistory(NegotiateHistory negotiateHistory) {
        return returnMapper.addNegotiateHistory(negotiateHistory);
    }

    @SneakyThrows
    @Override
    public Integer addNegotiateHistoryImage(int nhId, MultipartFile[] files, String[] instructions, HttpServletRequest request) {
        if (files != null && files.length > 0) {
            //循环获取file数组中得文件
            for (int i = 0; i < files.length; i++) {
                MultipartFile file = files[i];
                String uploadPath = request.getSession().getServletContext().getRealPath("/")+"static\\img\\tmall_return\\negotiate\\";
                File path =  new File(uploadPath);
                if(path == null){ path.mkdirs();}
                String filename = UUID.randomUUID().toString();
                // 通过MultipartFile的transferTo(File dest)这个方法来直接转存文件到指定的路径
                String ext =  file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
                System.err.println(ext);
                file.transferTo(new File(uploadPath+filename+file.getOriginalFilename()));
                // 打包成对象，交互sql
                NegotiateHistoryImages negotiateHistoryImages = new NegotiateHistoryImages();
                negotiateHistoryImages.setNhId(nhId);
                negotiateHistoryImages.setImageCode(i+1);
                negotiateHistoryImages.setImagePath("\\static\\img\\tmall_return\\negotiate\\"+filename+file.getOriginalFilename());
                negotiateHistoryImages.setImageInstruction(instructions[i]);
                // 添加
                returnMapper.addNegotiateHistoryImage(negotiateHistoryImages);
            }
        }
        return null;
    }


    @Override
    public PrimaryReturn findPrimaryReturnAllDetailByReturnOdd(String returnOdd) {
        PrimaryReturn primaryReturn = returnMapper.findPrimaryReturnAllDetailByReturnOdd(returnOdd);
        // 将某个状态的特定内容转换成集合键值对
        if(primaryReturn.getReturnStatusInfos().size() > 0){
            Map<String, ReturnStatusInfo> map = new HashMap<>();
            for (ReturnStatusInfo returnStatusInfo : primaryReturn.getReturnStatusInfos()) {
                map.put(returnStatusInfo.getReturnStatus()+"", returnStatusInfo);
            }
            primaryReturn.setReturnStatusInfoMap(map);
        }
        // 获取图片的键值对集合
        // 先获取图片的所有状态，进行一个无值的去重初始化
        if(primaryReturn.getPrtImages().size() > 0){
            Map<String, List<PrtImages>> map = new HashMap<>();
            for (PrtImages prtImage : primaryReturn.getPrtImages()) {
                map.put(/** set */prtImage.getReturnStatus()+"", new ArrayList<PrtImages>());
            }
            // 第二次循环给map值
            for (PrtImages prtImage : primaryReturn.getPrtImages()) {
                map.get(prtImage.getReturnStatus()+"").add(prtImage);
            }
            primaryReturn.setPrtImageMap(map);
            System.out.println("大王："+map.size());
            map.forEach((k,v) ->{
                System.out.println(k+"==="+v);
            });
        }
        return primaryReturn;
    }

    @Override
    public Integer updatePrimaryReturnStatusByReturnOdd(PrimaryReturn primaryReturn) {
        return returnMapper.updatePrimaryReturnStatusByReturnOdd(primaryReturn);
    }

    @Override
    public Integer findOrderOddByPrimaryReturnId(int primaryReturnId) {
        return returnMapper.findOrderOddByPrimaryReturnId(primaryReturnId);
    }

    @Override
    public PrimaryReturn findPrimaryReturnByMarketSrId(int msrId) {
        return returnMapper.findPrimaryReturnByMarketSrId(msrId);
    }
}
